﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Net.Mail;
using eschoolBO;

namespace ESCHOOL.admin
{
    public partial class WebForm23 : System.Web.UI.Page
    {
        Classes cl = new Classes(); student stud = new student(); parent par = new parent();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = cl.GetRecords(-99, "", -99, false, " and ");
                ddclass.DataSource = dt;
                ddclass.DataTextField = "class_name";
                ddclass.DataValueField = "class_id";
                ddclass.DataBind();
            }
        }

        protected void btnsend_Click(object sender, EventArgs e)
        {
            if (RadioButtonList1.SelectedValue == "Students")
            {
                DataTable dt = stud.GetRecords(-99, "", -99, Convert.ToInt64(ddclass.SelectedValue.ToString()), "", "", "", "", "", "", -99, -99, "", -99, "", "", "", "", false, " and ");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    MailMessage MyMessage = new MailMessage();
                    MailAddress SendFrom = new MailAddress("eschool123g@gmail.com");

                    if (FileUpload1.HasFile)
                        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/attachments/" + FileUpload1.FileName));

                    Attachment atc = new Attachment(Server.MapPath("~/admin/attachments/" + FileUpload1.FileName));
                    MyMessage.Attachments.Add(atc);


                    MyMessage.To.Add(dt.Rows[i]["student_emailid"].ToString());
                    MyMessage.From = SendFrom;
                    MyMessage.Subject = tbsubject.Text;
                    MyMessage.Body = tbmsg.Text;
                    System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("eschool123g@gmail.com", "eschool@123");

                    SmtpClient emailClient1 = new SmtpClient("smtp.gmail.com");
                    emailClient1.EnableSsl = true;
                    emailClient1.Port = 587;
                    emailClient1.Credentials = basicAuthenticationInfo;
                    emailClient1.Send(MyMessage);
                    //Response.Redirect("~/admin/Mailtogroup.aspx");
                     tbsubject.Text = ""; tbmsg.Text = ""; Label1.Visible = true;
                }
            }
            else
            {
                DataTable dt1= stud.GetRecords(-99, "", -99, Convert.ToInt64(ddclass.SelectedValue.ToString()), "", "", "", "", "", "", -99, -99, "", -99, "", "", "", "", false, " and ");
                DataTable dt = par.GetRecords(-99, "", "", "", "", "", "", "", -99, "", -99, "", "", Convert.ToInt64(dt1.Rows[0]["student_id"].ToString()), "", "", false, " and ");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    MailMessage MyMessage = new MailMessage();
                    MailAddress SendFrom = new MailAddress("eschool123g@gmail.com");

                    if (FileUpload1.HasFile)
                        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/admin/attachments/" + FileUpload1.FileName));

                    Attachment atc = new Attachment(Server.MapPath("~/admin/attachments/" + FileUpload1.FileName));
                    MyMessage.Attachments.Add(atc);


                    MyMessage.To.Add(dt.Rows[i]["student_emailid"].ToString());
                    MyMessage.From = SendFrom;
                    MyMessage.Subject = tbsubject.Text;
                    MyMessage.Body = tbmsg.Text;
                    System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("eschool123g@gmail.com", "eschool@123");

                    SmtpClient emailClient1 = new SmtpClient("smtp.gmail.com");
                    emailClient1.EnableSsl = true;
                    emailClient1.Port = 587;
                    emailClient1.Credentials = basicAuthenticationInfo;
                    emailClient1.Send(MyMessage);
                    //Response.Redirect("~/admin/Mailtogroup.aspx");
                   tbsubject.Text = ""; tbmsg.Text = ""; Label1.Visible = true;
                }
            }
        }
    }
}
